package ldh.im.fxbase.util;

import ldh.im.fxbase.richtext.TextItem;
import ldh.im.fxbase.richtext.TextItemType;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class ImRichTextUtil {

    public static List<Object> splitMessage(String content) {
        List<Object> result = new ArrayList<>();
        String reg = "￼[a-z]{1}\\{";
        List<String> strs = RegexUtil.split(reg, content);
        int preIndex = 0;
        for(int i=0, lg=strs.size(); i<lg; i++) {
            if (strs.get(i).startsWith(TextItem.SPLIT_STRING) && !strs.get(i).startsWith("￼e")) {
                String mergeStr = merge(strs, preIndex, i);
                if (mergeStr != null) {
                    result.add(mergeStr);
                }
                preIndex = i+2;
                parseAndAdd(result, strs.get(i), strs.get(i+1));
                i++;
            }
        }
        if (preIndex < strs.size()) {
            String mergeStr = merge(strs, preIndex, strs.size());
            if (mergeStr != null) result.add(mergeStr);
        }
        return result;
    }

    public static List<Object> split(String content) {
       List<Object> result = new ArrayList<Object>();
       List<String> strList = splitStrings(content);
       for(String str : strList) {
           if(str.startsWith(TextItem.SPLIT_STRING)) {
               TextItem textItem = TextItem.parse(str);
               if (textItem != null) {
                   result.add(textItem);
               }
           } else {
               result.add(str);
           }
       }
       return result;
    }

    public static List<String> splitStrings(String content) {
        List<String> result = new ArrayList<>();
        int length = content.length();
        int preIndex = 0;
        boolean isCheckSpan = false;
        for(int i=0; i<length; i++) {
            if (checkSpan(content, i)) {
                isCheckSpan = true;
                String preStr = content.substring(preIndex, i);
                if (!preStr.equals("")) {
                    result.add(preStr);
                }
                preIndex = i;
            } else if (content.charAt(i) == '}' && isCheckSpan) {
                isCheckSpan = false;
                String span = content.substring(preIndex, i+1);
                result.add(span);
                preIndex = i+1;
            }
        }
        if (preIndex < length) {
            String mergeStr = content.substring(preIndex, length);
            if (!mergeStr.equals("")) result.add(mergeStr);
        }
        return result;
    }

    public static int richTextType(String content) {
        if (content.indexOf(TextItem.SPLIT_STRING) < 0) {
            return 1;
        }
//        String[] strList = content.split(TextItem.SPLIT_STRING2);
        List<String> strList = RegexUtil.split(TextItem.SPLIT_REGEX, content);
        Map<String, Integer> countMap = new HashMap<>();
        int total = 0;
        for(String str : strList) {
            if (str.equals("")) continue;
            total++;
            boolean ishave = false;
            for(TextItemType textItemType : TextItemType.values()) {
                if (str.startsWith(textItemType.name())) {
                    ishave = true;
                    incr(countMap, textItemType.name());
                    break;
                }
            }
            if (!ishave) {
                incr(countMap, "str");
            }
        }
        if (countMap.containsKey("str") && total == countMap.get("str")) return 1; // 字符串
        if (countMap.containsKey(TextItemType.image.name()) && total == countMap.get(TextItemType.image.name()) && total == 1) return 2; // 图片
        return 3;   // richText
    }

    private static void incr(Map<String, Integer> countMap, String key) {
        Integer count = countMap.get(key);
        if (count == null) {
            count = 1;
            countMap.put(key, count);
        } else {
            count++;
            countMap.put(key, count);
        }
    }

    private static void parseAndAdd(List<Object> result, String str1, String str2) {
        int idx = str2.indexOf("}");
        if (idx > -1) {
            String s1 = str2.substring(0, idx+1);
            String s2 = str2.substring(idx+1);

            TextItem textItem = TextItem.parse(str1+s1);
            if (textItem != null) {
                result.add(textItem);
            }
            if (s2 != null && !s2.equals("")) {
                result.add(s2);
            }

        } else {
            TextItem textItem = TextItem.parse(str1+str2);
            if (textItem != null) {
                result.add(textItem);
            }
        }
    }

    private static String merge(List<String> strs, int startIndex, int endIndex) {
        if (startIndex >= endIndex) {
            return null;
        }
        return strs.subList(startIndex, endIndex).stream().collect(Collectors.joining());
    }

    private static boolean checkSpan(String content, int index) {
        if (index+3 > content.length()) return false;
        if (content.substring(index, index+1).equals(TextItem.SPLIT_STRING)) {
            if (isLowerCase(content.charAt(index+1))) {
                return content.charAt(index+2) == '{';
            }
        }
        return false;
    }

    private static boolean isLowerCase(char ch) {
        return Character.isLowerCase(ch);
    }

}
